package forum

import org.springframework.dao.DataIntegrityViolationException

class FrageController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max) {
        params.max = Math.min(max ?: 10, 100)
		if(params.katId>0){
			def k = Kategorie.get(params.katId)
			if(k==null)
				k = Kategorie.get(session.katId)
				def query = "from Frage as b where b.kategorie="+k.id+" order by b.dateCreated"
				def fragen = Frage.findAll(query)
				[frageInstanceList: fragen, frageInstanceTotal: Frage.count()]
		}else
        	[frageInstanceList: Frage.list(params), frageInstanceTotal: Frage.count()]
    }

    def create() {
        [frageInstance: new Frage(params)]
    }

    def save() {
        def frageInstance = new Frage(params)
        if (!frageInstance.save(flush: true)) {
            render(view: "create", model: [frageInstance: frageInstance])
            return
        }
        flash.message = message(code: 'default.created.message', args: [message(code: 'frage.label', default: 'Frage'), frageInstance.id])
        redirect(action: "list", id: frageInstance.id)
    }
//    def save() {
//    	println "params save= "+params
//    	def k
//    	try {
//    		k = Kategorie.get(params.katId as long)
//    	} catch (Exception e) {
//    		k = Kategorie.get(session.katId as long)
////			e.printStackTrace()
//    	}
//    	params.kategorie=k
//    			def ben = Benutzer.get(params.benutzer.id as long)
//    			params.benutzer=ben
//    			def frageInstance = new Frage(params)
//    	if (!frageInstance.save(flush: true)) {
//    		render(view: "create", model: [frageInstance: frageInstance])
//    		return
//    	}
//    	params.katId=k.id
//    			session.katId=k.id
//    			flash.message = message(code: 'default.created.message', args: [message(code: 'frage.label', default: 'Frage'), frageInstance.id])
////        redirect(action: "show", id: frageInstance.id)
//    			println "katId To Save :"+k.id
//    			redirect(action: "alle", katId:k.id)
//    }

    def show(Long id) {
        def frageInstance = Frage.get(id)
        if (!frageInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "list")
            return
        }

        [frageInstance: frageInstance]
    }

    def edit(Long id) {
        def frageInstance = Frage.get(id)
        if (!frageInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "list")
            return
        }

        [frageInstance: frageInstance]
    }

    def update(Long id, Long version) {
        def frageInstance = Frage.get(id)
        if (!frageInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (frageInstance.version > version) {
                frageInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'frage.label', default: 'Frage')] as Object[],
                          "Another user has updated this Frage while you were editing")
                render(view: "edit", model: [frageInstance: frageInstance])
                return
            }
        }

        frageInstance.properties = params

        if (!frageInstance.save(flush: true)) {
            render(view: "edit", model: [frageInstance: frageInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'frage.label', default: 'Frage'), frageInstance.id])
        redirect(action: "show", id: frageInstance.id)
    }

    def delete(Long id) {
        def frageInstance = Frage.get(id)
        if (!frageInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "list")
            return
        }

        try {
            frageInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'frage.label', default: 'Frage'), id])
            redirect(action: "show", id: id)
        }
    }
	
	def alle(){
//		println "params = "+params+ " session="+session.katId
//		params.katId=session.katId
		def k = Kategorie.get(params.katId)
		if(k==null)
			k = Kategorie.get(session.katId)
			
		def f = Frage.findAllWhere(kategorie:k)
		println "#### frage="+f+"  skatId="+session.katId+" user="+getAuthenticatedUser()+"  katId="+params.katId+"####"
		[fragen:f, katId:params.katId, user:getAuthenticatedUser()]

	}
	def alle_final(){
		def k = Kategorie.get(params.katId)
				def f = Frage.findAllWhere(kategorie:k)
				[fragen:f]
						
	}
	def all(Integer max){
		
	}
}
